package com.itheima.mobilesafe74.receiver;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;


import com.itheima.mobilesafe74.R;
import com.itheima.mobilesafe74.service.LocationService;
import com.itheima.mobilesafe74.utils.ConstantValue;
import com.itheima.mobilesafe74.utils.SpUtil;
import com.itheima.mobilesafe74.utils.ToastUtil;


public class SmsReceiver extends BroadcastReceiver {
    private ComponentName mDeviceAdminSample;
    private DevicePolicyManager mDPM;
    @Override
    public void onReceive(Context context, Intent intent) {
        mDPM = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        //1、判断是否开启防盗保护
        boolean open_security=SpUtil.getBoolean(context, ConstantValue.OPEN_SECURITY,false);

        if (open_security){
            //2、获取短信内容
            Object[] objects= (Object[]) intent.getExtras().get("pdus");
            //3、循环遍历短信
            for (Object object : objects) {
                //4、获取短信对象
                SmsMessage sms=SmsMessage.createFromPdu((byte[]) object);
                //5、获取短信对象的基本信息
                String originatingAddress=sms.getOriginatingAddress();
                String messageBody=sms.getMessageBody();

                //6、判断是否包含播放音乐的关键字
                if (messageBody.contains("#*alarm*#")){
                    //7、播放音乐（准备音乐，MediaPlayer）
                    MediaPlayer mediaPlayer=MediaPlayer.create(context, R.raw.ylzs);
                    mediaPlayer.setLooping(true);
                    mediaPlayer.start();
                }
                if(messageBody.contains("#*location*#")){
                    //8、开启获取位置服务
                    context.startService(new Intent(context,LocationService.class));
                }
                if (messageBody.contains("#*lockscreen*#")) {
                    //是否开启的判断
                    if (mDPM.isAdminActive(mDeviceAdminSample)) {
                        //激活--->锁屏
                        mDPM.lockNow();
                        //锁屏同时去设置密码
                        mDPM.resetPassword("123", 0);
                    } else {
                        ToastUtil.show(context, "请先激活");
                    }
                }
                if(messageBody.contains("#*wipedate*#")){
                    if(mDPM.isAdminActive(mDeviceAdminSample)){
                        mDPM.wipeData(0);//手机数据
//					mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//手机sd卡数据
                    }else{
                        ToastUtil.show(context, "请先激活");
                    }
                }
            }
        }
    }
}
